package com.departmentstore.controller;

import com.departmentstore.entity.Manufacturer;
import com.departmentstore.service.ManufacturerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/manufacturers")
@Tag(name = "制造商管理", description = "制造商的增删改查接口")
public class ManufacturerController {

    private final ManufacturerService manufacturerService;

    @Autowired
    public ManufacturerController(ManufacturerService manufacturerService) {
        this.manufacturerService = manufacturerService;
    }

    @PostMapping
    @Operation(summary = "创建制造商", description = "创建一个新的制造商")
    public ResponseEntity<Manufacturer> createManufacturer(@Valid @RequestBody Manufacturer manufacturer) {
        return ResponseEntity.ok(manufacturerService.createManufacturer(manufacturer));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新制造商", description = "根据ID更新制造商信息")
    public ResponseEntity<Manufacturer> updateManufacturer(
            @Parameter(description = "制造商ID") @PathVariable String id,
            @Valid @RequestBody Manufacturer manufacturer) {
        manufacturer.setManufacturerId(id);
        return ResponseEntity.ok(manufacturerService.updateManufacturer(manufacturer));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除制造商", description = "根据ID删除制造商")
    public ResponseEntity<Void> deleteManufacturer(
            @Parameter(description = "制造商ID") @PathVariable String id) {
        manufacturerService.deleteManufacturer(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取制造商", description = "根据ID获取制造商信息")
    public ResponseEntity<Manufacturer> getManufacturer(
            @Parameter(description = "制造商ID") @PathVariable String id) {
        return ResponseEntity.ok(manufacturerService.getManufacturerById(id));
    }

    @GetMapping
    @Operation(summary = "获取所有制造商", description = "获取所有制造商的列表")
    public ResponseEntity<List<Manufacturer>> getAllManufacturers() {
        return ResponseEntity.ok(manufacturerService.getAllManufacturers());
    }

    @GetMapping("/name/{name}")
    @Operation(summary = "根据名称查找制造商", description = "根据制造商名称查找制造商信息")
    public ResponseEntity<List<Manufacturer>> getManufacturersByName(
            @Parameter(description = "制造商名称") @PathVariable String name) {
        return ResponseEntity.ok(manufacturerService.getManufacturersByName(name));
    }

    @GetMapping("/address/{address}")
    public ResponseEntity<List<Manufacturer>> getManufacturersByAddress(@PathVariable String address) {
        return ResponseEntity.ok(manufacturerService.getManufacturersByAddress(address));
    }
} 